/*
 * 系统项目名称：综合决策分析系统-version2.0
 * 文件名：PortServiceImpl.java
 * 描述：
 * 版权：Copyright 2015-2020 义乌市跨境电商供应链管理有限公司-版权所有
 * 修改人：yangjx
 * 修改时间：2016-3-16 下午4:50:32
 * 版本号：V1.1
 */
package com.ywt.cda.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ywt.cda.action.model.PortsModel;
import com.ywt.cda.dao.Ana2ndDataDao;
import com.ywt.cda.dao.AnaImportDataDao;
import com.ywt.cda.domain.Ana2ndData;
import com.ywt.cda.domain.enums.AnaTable;
import com.ywt.cda.domain.enums.DimensionFlag;
import com.ywt.cda.domain.enums.ParamFlag;
import com.ywt.cda.service.GovAnalysisService;
import com.ywt.cda.service.PortService;
import com.ywt.cda.utils.DateUtils;

/**
 * （一句话功能简述） <br>
 * （功能详细描述）
 * 
 * @author yangjx
 * @version V1.0
 * @see [相关类/方法]
 * @since 2016-3-16 下午4:50:32
 */
@Service
public class PortServiceImpl implements PortService {

	@Resource
	private GovAnalysisService govAnalysisService;
	@Resource
	private Ana2ndDataDao ana2ndDataDao;
	@Resource
	private AnaImportDataDao anaImportDataDao;
	private final String YYYY_MM = "yyyy-MM";
	private final String CITY = "CITY";

	@Override
	public List<Ana2ndData> getExportData(PortsModel model) {

		AnaTable table = AnaTable.TB_ANA_DATA_2ND_GOV_D;
		if (YYYY_MM.equals(model.getStartTimePattern())) {
			model.setPageDatePattern("yyyy-mm");
			table = AnaTable.TB_ANA_DATA_2ND_GOV_M;
		}
		
		boolean isCity = CITY.equals(model.getCityOrPort());
		Map<String, Object> params = new HashMap<String, Object>();

		params.put("startDate", model.getStartTime_F());
		params.put("endDate", model.getEndTime_F());
		params.put("tableName", table.getTableName());
		params.put("mainFlag", DimensionFlag.EXPORT_PORT.getCode());

		params.put("isCity", isCity);
		if ((boolean) params.get("isCity")) {
			params.put("joinTableName", AnaTable.TB_CONF_DATA_AREA);
			params.put("condition", " A.MAIN_CODE = B.PORT_CODE ");
		}
		else {
			params.put("joinTableName", AnaTable.TB_CONF_DATA_PARAM);
			params.put("condition", " A.MAIN_CODE = B.CODE ");
			params.put("flag", ParamFlag.PORT_CODE.getFlag());
		}

		List<Ana2ndData> datas = ana2ndDataDao.listPort(params);

		return datas;
	}
	@Override
	public List<Ana2ndData> getImportData(PortsModel model) {

		AnaTable table = AnaTable.TB_ANA_IMPORT_DATA_D;
		if (YYYY_MM.equals(model.getStartTimePattern())) {
			model.setPageDatePattern("yyyy-mm");
			table = AnaTable.TB_ANA_IMPORT_DATA_M;
		}
		
		boolean isCity = CITY.equals(model.getCityOrPort());
		Map<String, Object> params = new HashMap<String, Object>();

		params.put("startDate", model.getStartTime_F());
		params.put("endDate", model.getEndTime_F());
		params.put("tableName", table.getTableName());
		params.put("dimensionFlag", DimensionFlag.EXPORT_PORT.getCode());

		params.put("isCity", isCity);
		if ((boolean) params.get("isCity")) {
			params.put("joinTableName", AnaTable.TB_CONF_DATA_AREA);
			params.put("condition", " A.DIMENSION_CODE = B.PORT_CODE ");
		}
		else {
			params.put("joinTableName", AnaTable.TB_CONF_DATA_PARAM);
			params.put("condition", " A.DIMENSION_CODE = B.CODE ");
			params.put("flag", ParamFlag.PORT_CODE.getFlag());
		}

		List<Ana2ndData> datas = anaImportDataDao.listPort(params);

		return datas;
	}
	

	@Override
	public void updateModel(PortsModel model) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
		Date today = new Date();

		String specifiedMonth = simpleDateFormat.format(today);
		if (null != model.getTimeSection()) {
			switch (model.getTimeSection()) {
			case SENVENDAY:
				model.setPageDatePattern("yyyy-mm-dd");
				break;
			case FIFTEENDAY:
				model.setPageDatePattern("yyyy-mm-dd");
				break;
			case THIRTYDAY:
				model.setPageDatePattern("yyyy-mm-dd");
				break;
			case SIXMONTH:

				model.setStartTime_F(DateUtils.getSpecifiedMonthBefore(
						specifiedMonth, 5));
				model.setEndTime_F(specifiedMonth);
				model.setStartTimePattern(YYYY_MM);
				model.setPageDatePattern("yyyy-mm");
				break;
			case NINEMONTH:

				model.setStartTime_F(DateUtils.getSpecifiedMonthBefore(
						specifiedMonth, 8));
				model.setEndTime_F(specifiedMonth);
				model.setStartTimePattern(YYYY_MM);
				model.setPageDatePattern("yyyy-mm");
				break;
			case TWELVEMONTH:
				model.setStartTime_F(DateUtils.getSpecifiedMonthBefore(
						specifiedMonth, 11));
				model.setEndTime_F(specifiedMonth);
				model.setStartTimePattern(YYYY_MM);
				model.setPageDatePattern("yyyy-mm");
				break;
			default:
				break;
			}
		}
	}

	@Override
	public void toJson(List<Ana2ndData> data, JSONObject jsonObject,
			PortsModel model) {

		Set<String> portOrCity = new HashSet<String>();
		for (Ana2ndData an : data) {
			portOrCity.add(an.getMainName());
		}

		Map<String, List<Ana2ndData>> groupByMainName = new HashMap<String, List<Ana2ndData>>();
		for (Ana2ndData an : data) {
			String key = an.getMainName();
			if (groupByMainName.containsKey(key)) {
				List<Ana2ndData> value = groupByMainName.get(key);
				value.add(an);
			}
			else {
				List<Ana2ndData> value = new ArrayList<Ana2ndData>();
				value.add(an);
				groupByMainName.put(key, value);
			}
		}

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
		Date today = new Date();

		String specifiedMonth = simpleDateFormat.format(today);
		List<String> dateList = null;
		if (null != model.getTimeSection()) {
			switch (model.getTimeSection()) {

			case SIXMONTH:
				dateList = DateUtils.getDateList(
						DateUtils.getSpecifiedMonthBefore(specifiedMonth, 5),
						specifiedMonth);

				break;
			case NINEMONTH:
				dateList = DateUtils.getDateList(
						DateUtils.getSpecifiedMonthBefore(specifiedMonth, 8),
						specifiedMonth);

				break;
			case TWELVEMONTH:
				dateList = DateUtils.getDateList(
						DateUtils.getSpecifiedMonthBefore(specifiedMonth, 11),
						specifiedMonth);

				break;
			default:
				dateList = DateUtils.getDateList(model.getStartTime_F(),
						model.getEndTime_F());
				break;
			}
		}
		else {
			dateList = DateUtils.getDateList(model.getStartTime_F(),
					model.getEndTime_F());
		}
		JSONArray nameArray = new JSONArray();
		for (String date : dateList) {
			nameArray.add(date);
		}
		JSONArray legendArray =new  JSONArray();
		for(String l:groupByMainName.keySet()){
			legendArray.add(l);
		}
		Iterator<Entry<String, List<Ana2ndData>>> iterator = groupByMainName
				.entrySet().iterator();
		// 票数
		JSONArray arrayTotal = new JSONArray();
		// 金额
		JSONArray array1Total = new JSONArray();
		// 商品件数
		JSONArray array2Total = new JSONArray();
		// 客单价
		JSONArray array3Total = new JSONArray();
		while (iterator.hasNext()) {
			// 每个关区或者城市对应时间段的数据
			Entry<String, List<Ana2ndData>> item = iterator.next();
			String key = item.getKey();
			List<Ana2ndData> value = item.getValue();
			JSONObject ojb = new JSONObject();
			JSONObject ojb1 = new JSONObject();
			JSONObject ojb2 = new JSONObject();
			JSONObject ojb3 = new JSONObject();
			Map<String, Ana2ndData> temp = new TreeMap<String, Ana2ndData>();
			for (Ana2ndData a : value) {
				if (!temp.containsKey(a.getDateFlag())) {
					temp.put(a.getDateFlag(), a);
				}
			}

			// 补全数据
			for (String date : dateList) {
				if (null == temp.get(date)) {
					Ana2ndData d = new Ana2ndData();
					d.setSumNum(new BigDecimal(0));
					d.setSumPrice(new BigDecimal(0));
					d.setSumPkg(new BigDecimal(0));
					d.setAvgPrice(new BigDecimal(0));
					temp.put(date, d);
				}
			}

			JSONObject obj0 = new JSONObject();
			JSONObject obj01 = new JSONObject();
			JSONObject obj02 = new JSONObject();
			JSONObject obj03 = new JSONObject();
			
			JSONObject obj2 = new JSONObject();
			JSONObject obj21 = new JSONObject();
			JSONObject obj22 = new JSONObject();
			JSONObject obj23 = new JSONObject();
			
			JSONObject obj3 = new JSONObject();
			JSONObject obj3_1 = new JSONObject();
			JSONObject obj3_2 = new JSONObject();
			JSONObject obj3_3 = new JSONObject();
			
			JSONObject obj31 = new JSONObject();
			JSONObject obj31_1 = new JSONObject();
			JSONObject obj31_2 = new JSONObject();
			JSONObject obj31_3 = new JSONObject();
			JSONObject obj4 = new JSONObject();
			JSONObject obj41 = new JSONObject();
			JSONObject obj42 = new JSONObject();
			JSONObject obj43 = new JSONObject();
			
			JSONObject obj5 = new JSONObject();
			JSONObject obj51 = new JSONObject();
			JSONObject obj52 = new JSONObject();
			JSONObject obj53 = new JSONObject();
			String r = getRandom();
			String r1 = getRandom();
			String r2 = getRandom();
			String r3 = getRandom();
			
			String g = getRandom();
			String g1 = getRandom();
			String g2 = getRandom();
			String g3 = getRandom();
			String b = getRandom();
			String b1= getRandom();
			String b2 = getRandom();
			String b3 = getRandom();
			String a=getRandoma();
			
			String a1=getRandoma();
			String a2=getRandoma();
			String a3=getRandoma();
			
			String rgb = r + "," + g + "," + b;
			String rgb1 = r1 + "," + g1 + "," + b1;
			String rgb2 = r2 + "," + g2 + "," + b2;
			String rgb3 = r3 + "," + g3 + "," + b3;
			rgb=rgb+","+a;
			rgb1=rgb1+","+a1;
			rgb2=rgb2+","+a2;
			rgb3=rgb3+","+a3;
			obj4.put("color", "rgba("+rgb+")");
			
			obj41.put("color", "rgba("+rgb1+")");
			obj42.put("color", "rgba("+rgb2+")");
			obj43.put("color", "rgba("+rgb3+")");
			obj3.put("show", true);
			obj3_1.put("show", true);
			obj3_2.put("show", true);
			obj3_3.put("show", true);
			obj31.put("show", true);
			obj31_1.put("show", true);
			obj31_2.put("show", true);
			obj31_3.put("show", true);
			
			obj5.put("label", obj31);
			obj51.put("label", obj31_1);
			obj52.put("label", obj31_2);
			obj53.put("label", obj31_3);
			
			obj2.put("label", obj3);
			obj2.put("areaStyle", obj4);
			obj2.put("color", "rgba("+rgb+")");
			
			obj21.put("label", obj3_1);
			obj21.put("areaStyle", obj41);
			obj21.put("color", "rgba("+rgb1+")");
			
			obj22.put("label", obj3_2);
			obj22.put("areaStyle", obj42);
			obj22.put("color", "rgba("+rgb2+")");
			
			obj23.put("label", obj3_3);
			obj23.put("areaStyle", obj43);
			obj23.put("color", "rgba("+rgb3+")");
			
			obj0.put("emphasis", obj5);
			obj0.put("normal", obj2);
			obj01.put("emphasis", obj51);
			obj01.put("normal", obj21);
			obj02.put("emphasis", obj52);
			obj02.put("normal", obj22);
			obj03.put("emphasis", obj53);
			obj03.put("normal", obj23);
			
			ojb.put("itemStyle", obj0);
			ojb.put("name", key);

			ojb.put("type", "line");

			ojb.put("smooth", true);

			ojb1.put("itemStyle", obj01);
			ojb1.put("name", key);

			ojb1.put("type", "line");

			ojb1.put("smooth", true);

			ojb2.put("itemStyle", obj02);
			ojb2.put("name", key);

			ojb2.put("type", "line");

			ojb2.put("smooth", true);

			ojb3.put("itemStyle", obj03);
			ojb3.put("name", key);

			ojb3.put("type", "line");

			ojb3.put("smooth", true);

			Iterator<Entry<String, Ana2ndData>> iteratorTemp = temp.entrySet()
					.iterator();
			// 票数
			JSONArray array = new JSONArray();
			// 金额
			JSONArray array1 = new JSONArray();
			// 商品件数
			JSONArray array2 = new JSONArray();
			// 客单价
			JSONArray array3 = new JSONArray();
			while (iteratorTemp.hasNext()) {
				Entry<String, Ana2ndData> i = iteratorTemp.next();
				Ana2ndData d = i.getValue();
				array.add(d.getSumNum());
				array1.add(d.getSumPrice_());
				array2.add(d.getSumPkg_());
				array3.add(d.getAvgPrice());
			}
			ojb.put("data", array);
			ojb1.put("data", array1);
			ojb2.put("data", array2);
			ojb3.put("data", array3);
			arrayTotal.add(ojb);
			array1Total.add(ojb1);
			array2Total.add(ojb2);
			array3Total.add(ojb3);
		}

		jsonObject.put("numSeries", arrayTotal);
		jsonObject.put("priceSeries", array1Total);
		jsonObject.put("pkgSeries", array2Total);
		jsonObject.put("avgPriceSeries", array3Total);
		jsonObject.put("nameArray", nameArray);
		jsonObject.put("legendArray", legendArray);
	}
	private static String getRandoma() {
		// TODO Auto-generated method stub
		String[] a =new String[]{"0.1","0.2","0.3","0.4","0.5","0.6","0.7","0.8","0.9"};
		return a[RandomUtils.nextInt(0, a.length)];
	
	}
	private static String getRandom() {
		return String.valueOf(RandomUtils.nextInt(1, 254));
	}
	public static void main(String[] args) {
		System.out.println(getRandoma());
	}
}
